package com.javaclimb.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.javaclimb.music.domain.Rank;
import com.javaclimb.music.service.RankService;
import com.javaclimb.music.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
public class RankController {

    @Autowired
    private RankService rankService;

    /**
     * 新增评价
     */
    @RequestMapping(value = "/rank/add", method = RequestMethod.POST)
    public Object add(Rank rank) {
        JSONObject jsonObject = new JSONObject();

        boolean flag = rankService.save(rank);
        if (flag) {
            return R.ok().put("data", rank);
        }
        return R.error();
    }

    /**
     * 计算平均分
     */
    @RequestMapping(value = "/rank", method = RequestMethod.GET)
    public Object rankOfSongListId(HttpServletRequest request) {
        String songListId = request.getParameter("songListId");
        return R.ok().put("data", rankService.rankOfSongListId(Integer.parseInt(songListId)));
    }
}






















